package model.bo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import model.bean.DailyLog;
import model.bean.Service;
import model.bean.ServiceInfo;
import model.bean.TourInfo;
import model.bean.TourKind;
import model.bean.TourTemp;
import model.bean.TourTemplate;
import model.dao.TourUpdateDAO;

public class TourUpdateBO {
	TourUpdateDAO dao = null;

	public boolean insertTour(String tourTempName, int tourKind,
			double tourPrice, int usual) {
		// TODO Auto-generated method stub
		dao = new TourUpdateDAO();
		boolean insert = dao.insertTour(tourTempName, tourKind, tourPrice, usual);
		return insert;
	}

	public ArrayList<TourKind> GetTourKind() {
		// TODO Auto-generated method stub
		dao = new TourUpdateDAO();
		ArrayList<TourKind> kind = dao.getTourKind();
		return kind;
	}

	public ArrayList<TourTemp> getListTour() {
		// TODO Auto-generated method stub
		dao = new TourUpdateDAO();
		ArrayList<TourTemp>  listTour = null;
		listTour = dao.getListTour();
		return listTour;
	}

	public boolean updateTour(int tourTempIndex, String tourTempName,
			int tourKind, double tourPrice, int usual) {

		dao = new TourUpdateDAO();
		boolean update = dao.updateTour(tourTempIndex, tourTempName, tourKind, tourPrice, usual);
		return update;
	}

	public ArrayList<DailyLog> getDailyLog(int tourId) {
		// TODO Auto-generated method stub
		dao = new TourUpdateDAO();
		ArrayList<DailyLog> dailyLog = dao.getDailyLog(tourId);
		return dailyLog;
	}

	public ArrayList<Service> getService(int i) {
		// TODO Auto-generated method stub
		dao = new TourUpdateDAO();
		ArrayList<Service> service = dao.getService(i);
		return service;
	}

	public boolean insertDailyLog(int tourId, int target, int traffic, int res,
			int hotel, int day) {
		// TODO Auto-generated method stub
		dao = new TourUpdateDAO();
		boolean dailyInsert = dao.insertDailyLog(tourId, target, traffic, res, hotel, day);
		return dailyInsert;
	}
	 public ArrayList<TourInfo> getTourList(String dayFirst, String dayUp) {
	        // TODO Auto-generated method stub
	        Calendar today = Calendar.getInstance();
	        TourUpdateDAO dao = new TourUpdateDAO();
	        dayFirst = dayFormat(dayFirst,"2010-10-10");
	        String year = String.valueOf(today.get(Calendar.YEAR));
	        String month = String.valueOf((Calendar.MONTH<9?"0":"")+
	                String.valueOf(today.get(Calendar.MONTH)+1));
	        String day = String.valueOf((Calendar.DAY_OF_MONTH<10?"0":"")+
	                String.valueOf(today.get(Calendar.DAY_OF_MONTH)));
	        String currentDay = year+"-"+month+"-"+day;
	        dayUp = dayFormat(dayUp,currentDay);
	        
	        ArrayList<TourInfo> listTour = dao.getTourList(dayFirst, dayUp);
	        return listTour;
	    }
	 private String dayFormat(String inputDay, String defaultDay){
	        SimpleDateFormat formatStr = new SimpleDateFormat("dd-MM-yyyy");
	        Date date;
	        String dateFormat = "";
	        inputDay = inputDay == null?"10-10-2010":inputDay;
	        try {
	            date = formatStr.parse(inputDay);
	            formatStr.applyPattern("yyyy-MM-dd");
	            dateFormat = formatStr.format(date);
	            System.out.println(inputDay+" df "+defaultDay);            
	        } catch (ParseException e) {
	            e.printStackTrace();
	            System.out.println(defaultDay);
	            dateFormat = defaultDay;
	        }
	        System.out.println(inputDay+" df "+dateFormat);
	        return dateFormat;
	    }
	 public ArrayList<TourTemplate> getTempList() {
	        // TODO Auto-generated method stub
	        dao = new TourUpdateDAO();
	        ArrayList<TourTemplate> listTempTour = dao.getTempList();
	        return listTempTour;
	    }
	 public boolean updateTourExac(int tourIndex,
	            int tourTempIndex, String dayStart, String dayEnd) {
	        // TODO Auto-generated method stub
	        dao = new TourUpdateDAO();
	        //TourInfo tour = dao.getTourInfo(tourIndex);
	        
	        //check modified infor
	        dayStart = dayFormat(dayStart,"2010-10-10");
	        dayEnd = dayFormat(dayEnd, "2010-10-10");
	        System.out.println(dayStart+" : "+dayEnd);
	        boolean updateTour = false;
	        updateTour = dao.updateTourExac(tourIndex, tourTempIndex, dayStart, dayEnd);
	        System.out.println("da thuc hien den TourUpdateBO");
	        return updateTour;
	    }
	 public ArrayList<ServiceInfo> getServiceList(int type, String dayStart,
	            String dayEnd, String order) {
	        
	        Calendar today = Calendar.getInstance();
	        TourUpdateDAO dao = new TourUpdateDAO();
	        dayStart = dayFormat(dayStart,"2010-10-10");
	        String year = String.valueOf(today.get(Calendar.YEAR));
	        String month = String.valueOf((Calendar.MONTH<9?"0":"")+
	                String.valueOf(today.get(Calendar.MONTH)+1));
	        String day = String.valueOf((Calendar.DAY_OF_MONTH<10?"0":"")+
	                String.valueOf(today.get(Calendar.DAY_OF_MONTH)));
	        String currentDay = year+"-"+month+"-"+day;
	        dayEnd = dayFormat(dayEnd,currentDay);
	        
	        ArrayList<ServiceInfo> listService = dao.getServiceList(type, dayStart, dayEnd, order);
	        
	        return listService;
	        
	    }


}
